package com.lfg.leetcode.leetcode100;

public class TreeLinkNode {
    public int val;
    public TreeLinkNode left, right, next;

    public TreeLinkNode(int x) {
        val = x;
    }

    public static TreeLinkNode stringToTreeLinkNode(String content) {
        String[] str = content.split(",");
        return stringToTreeLinkNode(str, 0);
    }

    private static TreeLinkNode stringToTreeLinkNode(String[] str, int index) {
        if ("#".equals(str[index])) {
            return null;
        }
        TreeLinkNode node = new TreeLinkNode(Integer.valueOf(str[index]));
        int left = index * 2 + 1;
        if (left < str.length) {
            node.left = stringToTreeLinkNode(str, left);
        }
        int right = index * 2 + 2;
        if (right < str.length) {
            node.right = stringToTreeLinkNode(str, right);
        }
        return node;
    }
}